package com.sixmac.controller.backend;

import com.sixmac.common.DataTableFactory;
import com.sixmac.controller.common.CommonController;
import com.sixmac.entity.Info;
import com.sixmac.entity.WaterPrice;
import com.sixmac.service.InfoService;
import com.sixmac.service.OperaService;
import com.sixmac.service.WaterPriceService;
import com.sixmac.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/30 0030.
 */
@Controller
@RequestMapping(value = "backend/info")
public class InfoController extends CommonController {

    @Autowired
    private InfoService infoService;

    @Autowired
    private OperaService operaService;

    @Autowired
    private WaterPriceService waterPriceService;

    @RequestMapping("/index")
    public String index(ModelMap model, Integer index) {
        model.addAttribute("index", index);
        setModel(model, index);
        return "backend/公共信息列表";
    }

    @RequestMapping("/add")
    public String add(ModelMap model, Integer index) {
        model.addAttribute("index", index);
        model.addAttribute("infoType", index);
        setModel(model, index);
        return "backend/编辑公共信息";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     Integer pageIndex,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);

        Page<Info> page = infoService.pageByIndex(pageIndex, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("/show")
    public String show(ModelMap model, Integer id) {
        Info info = infoService.getById(id);
        model.addAttribute("info", info);
        setModel(model, info.getType());
        return "backend/查看公共信息";
    }

    @RequestMapping("/edit")
    public String edit(ModelMap model, Integer id, Integer type) {
        Info info = infoService.getById(id);

        if (null != info) {
            model.addAttribute("info", info);
            setModel(model, info.getType());
        }

        model.addAttribute("infoType", type);

        return "backend/编辑公共信息";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Integer save(HttpServletRequest request, Integer id, Integer type, String name, String content) {
        try {
            Info info = null;

            if (null == id) {
                info = new Info();
                info.setType(type);
            } else {
                info = infoService.getById(id);
            }

            info.setName(name);
            info.setDescription(content);
            info.setUpdateTime(new Date());

            infoService.update(info);

            operaService.addOperaInfo(request, (null == id ? "添加" : "修改") + getTypeName(info.getType()) + " " + info.getName());

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(HttpServletRequest request, Integer id) {
        try {
            Info info = infoService.getById(id);

            operaService.addOperaInfo(request, "删除" + getTypeName(info.getType()) + " " + info.getName());

            infoService.deleteById(id);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    private String getTypeName(Integer type) {
        String text = "";
        switch (type) {
            case 1:
                text = "用水常识";
                break;
            case 2:
                text = "业务办理流程";
                break;
            case 3:
                text = "缴费方式";
                break;
        }

        return text;
    }

    private void setModel(ModelMap model, Integer index) {
        switch (index) {
            case 1:
                model.addAttribute("showText", "用水常识");
                break;
            case 2:
                model.addAttribute("showText", "业务办理流程");
                break;
            case 3:
                model.addAttribute("showText", "缴费方式");
                break;
        }
    }

    // ***********************************************************水价信息***********************************************************
    @RequestMapping("/priceInfo")
    public String priceInfo(ModelMap model, Integer id) {
        WaterPrice waterPrice = waterPriceService.getById(id);

        if (null != waterPrice) {
            model.addAttribute("waterPrice", waterPrice);
        }

        return "backend/编辑水价信息";
    }

    @RequestMapping("/saveWaterPrice")
    @ResponseBody
    public Integer saveWaterPrice(HttpServletRequest request, Integer id, String content) {
        try {
            WaterPrice waterPrice = waterPriceService.getById(id);
            waterPrice.setDescription(content);
            waterPriceService.update(waterPrice);

            operaService.addOperaInfo(request, "修改水价信息");

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
